package com.aysafety.iotdataplatformweb.controller.gas;

import com.aysafety.core.common.CheckUtils;
import com.aysafety.core.common.Common;
import com.aysafety.core.common.DataConversionUtil;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.gas.GasService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController @RequestMapping("/api/mineMonitor/realMonitor/gasMonitor/") public class GasController
    extends BaseController {

    @Autowired GasService gasService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/getGasRealTime")
    public Map<String, Object> getGasRealTime(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("mineId") || !params.containsKey("equipmentId")) {
                return this.formatReturnData("缺少参数");
            }
            String mineId = params.get("mineId").toString();
            String equipmentId = params.get("equipmentId").toString();
            String startTime = params.containsKey("startTime") ?
                params.get("startTime").toString() :
                DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ?
                params.get("endTime").toString() :
                DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String timeSpanStr = params.containsKey("timeSpan") ? params.get("timeSpan").toString() : "0";
            timeSpanStr = StringUtils.isNotBlank(timeSpanStr) ? timeSpanStr : "0";
            int timeSpan = Integer.parseInt(timeSpanStr);
            String timeUnit = params.containsKey("timeUnit") ? params.get("timeUnit").toString() : "s";
            timeUnit = StringUtils.isNotBlank(timeUnit) ? timeUnit : "s";
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "10000";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "10000";
            //int size = Integer.parseInt(sizeStr);
            int size = 10000;
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            String firstStartRow = params.containsKey("firstStartRow") ? params.get("firstStartRow").toString() : "";
            data.put("total", gasService.countByGas(mineId, equipmentId, startTime, endTime, timeSpan, timeUnit));
            result = Common.getData(gasService
                .queryRealTimeByGas(mineId, equipmentId, startTime, endTime, timeSpan, timeUnit, page, size + 1,
                    startRow), size, startRow, firstStartRow);
            data.put("list", result.get("list"));
            data.put("firstStartRow", result.get("firstStartRow"));
            data.put("lastStartRow", result.get("lastStartRow"));
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("GasController-getGasRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("GasController-getGasRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    @RequestMapping("/getCalGas")
    public Map<String, Object> getCalGas(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            String entId = "";
            if (!params.containsKey("mineId") || StringUtils.isBlank(params.get("mineId").toString())) {
                return this.formatReturnData("mineId不能为空");
            }
            entId = params.get("mineId").toString();
            paramsMap.put("entId", entId);

            if (params.containsKey("equipmentId")) {
                String deviceId = params.get("equipmentId").toString();
                paramsMap.put("deviceId", deviceId);
            }


            String startTime = params.containsKey("startTime") ?
                params.get("startTime").toString() :
                DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ?
                params.get("endTime").toString() :
                DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");


            paramsMap.put("startTime", startTime);
            paramsMap.put("endTime", endTime);
            //            if(params.containsKey("dt")){
            //                String dt = params.get("dt").toString();
            //                paramsMap.put("dt",dt);
            //            }
            //
            //            if(params.containsKey("m")){
            //                String m = params.get("m").toString();
            //                paramsMap.put("m",m);
            //            }
            //
            //            if(params.containsKey("n")){
            //                String n = params.get("n").toString();
            //                paramsMap.put("n",n);
            //            }
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "10000";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "10000";
            //int size = Integer.parseInt(sizeStr);
            int size = 10000;
            paramsMap.put("page", page);
            paramsMap.put("size", size);
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            paramsMap.put("startRow", startRow);
            String firstStartRow = params.containsKey("firstStartRow") ? params.get("firstStartRow").toString() : "";
            paramsMap.put("firstStartRow", firstStartRow);

            data.put("total", gasService.countByCalGas(paramsMap));
            List<Map<String, Object>> data1 = gasService.queryCalGas(paramsMap);
            if (params.containsKey("s") && StringUtils.isNotBlank(params.get("s").toString())) {
                for (Map<String, Object> map : data1) {
                    map.put("gasAbsEmission", Double.parseDouble(map.get("gasAbsEmission").toString()) * Double
                        .parseDouble(params.get("s").toString()));
                }
            }
            result = Common.getData(data1, size, startRow, firstStartRow);
            data.put("list", result.get("list"));
            data.put("firstStartRow", result.get("firstStartRow"));
            data.put("lastStartRow", result.get("lastStartRow"));
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("GasController-getCalGas:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("GasController-getCalGas:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
     * @Description: 综合预警
     * @Param: [params, request, response]
     * @return: java.util.Map<java.lang.String , java.lang.Object>
     * @Author: ChengQi
     * @Date: 2019/1/3
     */
    @RequestMapping("/comprehensiveWarning") public Map<String, Object> comprehensiveWarning(
        @RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();

            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);

            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);

            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = gasService.comprehensiveWarning(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }
}
